package main

import (
	"fmt"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	"github.com/martini-contrib/sessions"
	"html/template"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"time"
)

type CodeMsg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}
type LogModel struct {
	Name   string        `json:"name"`
	Type   string        `json:"type"`
	Start  int64         `json:"start"`
	Length int           `json:"length"`
	Data   template.HTML `json:"data"`
}

type RequestModel struct {
	request *http.Request
	r       render.Render
	session sessions.Session
	params  martini.Params
}
type Render func(RequestModel)
type Excutor struct {
	rd Render
}

func (exe Excutor) render(request *http.Request, r render.Render, session sessions.Session, params martini.Params) {

	exe.rd(RequestModel{request: request, r: r, session: session, params: params})
}

type UserInfo struct {
	UserName    string `json:"username"`
	Password    string `json:"password"`
	Permissions string `json:"permissions"`
}
type JobInfo struct {
	Name             string           `json:"name"`
	RunDir           string           `json:"run_dir"`
	RunCmd           string           `json:"run_cmd"`
	RunCmdExt        string           `json:"run_cmd_ext"`
	RunLog           string           `json:"run_log"`
	HookCmd          string           `json:"hook_cmd"`
	HookCmdExt       string           `json:"hook_cmd_ext"`
	HookLog          string           `json:"hook_log"`
	HookDir          string           `json:"hook_dir"`
	RunStatus        int              `json:"-"`
	HookStatus       int              `json:"-"`
	RunProcess       *os.Process      `json:"-"`
	HookProcess      *os.Process      `json:"-"`
	RunProcessState  *os.ProcessState `json:"-"`
	HookProcessState *os.ProcessState `json:"-"`
	LastErrors       []error          `json:"-"`
	RunLogFile       *os.File         `json:"-"`
	HookLogFile      *os.File         `json:"-"`
}

func dateString() string {
	return time.Now().Format("20060102")
}
func regexpMach(exp string, data string) bool {
	result, err := regexp.MatchString(exp, data)
	if debug {
		fmt.Println(exp, "====", data, result, err)
	}
	if err != nil {
		return false
	}
	return result
}

func typeOfStatus(status int) string {
	switch status {
	case -1:
		return "运行失败(" + strconv.Itoa(status) + ")"
	case 0:
		return "未启动(" + strconv.Itoa(status) + ")"
	case 1:
		return "运行中(" + strconv.Itoa(status) + ")"
	case 2:
		return "停止中(" + strconv.Itoa(status) + ")"
	case 3:
		return "完成(" + strconv.Itoa(status) + ")"
	case 4:
		return "已终止(" + strconv.Itoa(status) + ")"
	case 5:
		return "异常终止(" + strconv.Itoa(status) + ")"
	default:
		return "未知(" + strconv.Itoa(status) + ")"
	}
}

type PageData struct {
	Title string
	Body  map[string]interface{}
}

func (page PageData) setTitle(title string) {
	page.Title = title
}
func (page PageData) put(key string, value interface{}) {
	if page.Body == nil {
		page.Body = make(map[string]interface{})
	}
	page.Body[key] = value
}
func getDefaltPageData(title string, model RequestModel) PageData {
	page := PageData{Title: title, Body: map[string]interface{}{}}
	if model.session.Get("user") != nil {
		page.put("user", model.session.Get("user"))
	}
	return page
}

func (job JobInfo) pushError(er error) {
	lens := len(job.LastErrors)
	if lens >= 10 {
		job.LastErrors = job.LastErrors[lens-10:]
	}
	job.LastErrors = append(job.LastErrors, er)
}
